package coin.yang;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class setEx {
    public static void main(String[] args) {

        Set<Student> set = new TreeSet<>();
        // 添加元素
        set.add(new Student("Daniel", 17, 82));
        set.add(new Student("Amy", 15, 61));
        set.add(new Student("Fed", 18, 81));
        set.add(new Student("Bob", 14, 59));
        set.add(new Student("Can", 16, 77));
        set.add(new Student("Grace", 19, 70));
        set.add(new Student("Evan", 20, 48));
        System.out.println(set);

        Set<String> set2 = new HashSet<>();
        // 添加元素
        set2.add("David");
        set2.add("Bill");
        set2.add("Alex");
        set2.add("Jones");
        set2.add("Benny");
        set2.add("Adair");
        set2.add("Hack");
    }

}

class Student implements Comparable<Student> {

    private String name;
    private int age;
    private int score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    // 在这个方法中需要指定比较规则，根据规则来决定是否交换
    // 按照年龄进行升序排序
    // 升序：this-o
    // 降序：o-this
    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
}
